<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <title>任务日志列表 Task Log</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all">
  <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">按任务搜索</label>
              <div class="layui-input-block">
                <select name="tid" lay-verify="" id="layui-form-tid">
                    <option value="0">所有任务</option>
                  <option  th:each="t:${tasks}" th:value="${t.id}" th:text="${'['+t.group + ']' + t.name}" th:selected="${tid != null && tid > 0 && t.id == tid}"></option>
                </select>
              </div>
            </div>
            <button class="layui-btn" data-type="search">搜索</button>
          </div>
        </div>
        <div class="layui-card-body">

          <table class="layui-hide" id="task-log-table" lay-filter="task-log-filter"></table>

          <script type="text/html" id="task-log-table-toolbar">
            <div class="layui-btn-container">
              <button class="layui-btn layui-btn-sm" lay-event="clear">清空</button>
              <button class="layui-btn layui-btn-sm" lay-event="setting">设置</button>
            </div>
          </script>

        </div>
      </div>
    </div>
  </div>
</div>


<script th:src="@{/statics/layui/layui.js}"></script>
<script th:inline="none">
  layui.config({
    base: '/statics/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table'], function(){
    var admin = layui.admin
    ,table = layui.table;

      var tableIns = table.render({
        elem: '#task-log-table'
        ,url: '/api/taskLog/page'
        ,id:'id'
        ,toolbar: '#task-log-table-toolbar'
        ,cols: [[
          {field:'taskId', width:80, title: '任务ID'}
          ,{field:'taskGroup', width:100, title: '任务组名', sort: true}
          ,{field:'taskName', title: '任务名称'}
          ,{field:'duration', width:100, title: '执行时长', templet:function (d) {
              return d.duration + ' ms';
          }}
          ,{field:'success', width:100, title: '结果',templet:function (d) {
              if(d.success == 1){
                  return '<span class="layui-btn layui-btn-xs"> 成功 </span>';
              }else{
                  return '<span class="layui-btn layui-btn-xs layui-btn-danger"> 失败 </span>';
              }
          }}
          ,{field:'createTime', width:180, title: '时间', sort: true}
        ]]
        ,page: true
        ,request: {
            pageName:'pageNum'
            ,limitName: 'pageSize'
        }
    });

      //头工具栏事件
      table.on('toolbar(task-log-filter)', function(obj){
          var checkStatus = table.checkStatus('id');
          switch(obj.event){
              case 'clear':
                  //var data = checkStatus.data;
                  //layer.msg('选中了：'+ data.length + ' 个');
                  $.ajax({
                      url: '/api/taskLog/clear',
                      method: 'DELETE',
                      dataType: 'JSON',
                      success: function (res) {
                          if (res.code == '0') {
                              tableIns.reload({page: {curr: 1}}); //数据刷新
                          } else {
                              layer.msg(res.msg);
                          }
                      },
                      error: function (err) {
                      }
                  });
                  break;
              case 'setting':
                  //layer.msg(checkStatus.isAll ? '全选': '未全选');
                  layer.open({
                      type: 2
                      ,title: '系统设置'
                      ,content: '/cms/sys/setting'
                      ,maxmin: true
                      ,area: ['600px', '360px']
                      ,btn: ['确定', '取消']
                      ,yes: function(index, layero){
                          var iframeWindow = window['layui-layer-iframe'+ index]
                              ,submitID = 'LAY-sys-setting-submit'
                              ,submit = layero.find('iframe').contents().find('#'+ submitID);

                          //监听提交
                          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                              var field = data.field; //获取提交的字段
                              if(field.sys_check_code == "on") {
                                  field.sys_check_code = "1";
                              } else {
                                  field.sys_check_code = "0";
                              }
                              if(field.sys_aliyun_oss_enable == "on") {
                                  field.sys_aliyun_oss_enable = "1";
                              } else {
                                  field.sys_aliyun_oss_enable = "0";
                              }

                              //layer.msg(JSON.stringify(data.field));
                              //console.log(field);
                              //提交 Ajax 成功后，静态更新表格中的数据
                              $.ajax({
                                  url: '/api/config/save',
                                  method: 'post',
                                  data: {
                                      sys_check_code: field.sys_check_code,
                                      sys_log_method: field.sys_log_method,
                                      sys_log_dates: field.sys_log_dates,
                                      sys_aliyun_oss_enable: field.sys_aliyun_oss_enable,
                                      sys_aliyun_oss_endpoint: field.sys_aliyun_oss_endpoint,
                                      sys_aliyun_oss_keyid: field.sys_aliyun_oss_keyid,
                                      sys_aliyun_oss_keysecret: field.sys_aliyun_oss_keysecret,
                                      sys_aliyun_oss_bucketname: field.sys_aliyun_oss_bucketname,
                                      sys_aliyun_oss_filehost: field.sys_aliyun_oss_filehost
                                  },
                                  dataType:'JSON',
                                  traditional :true,
                                  success:function(res){
                                      if(res.code=='0'){
                                          tableIns.reload(); //数据刷新
                                          layer.close(index); //关闭弹层
                                      }else {
                                          alert(res.msg);
                                      }
                                  },
                                  error:function (err) {
                                  }
                              });

                          });

                          submit.trigger('click');
                      }
                  });
                  break;
          };
      });

      var $ = layui.$, active = {
          search: function(){
              var tidField = $('#layui-form-tid');
              //执行重载
              tableIns.reload({
                  page: {
                      curr: 1 //重新从第 1 页开始
                  }
                  ,where: {
                      tid: tidField.val()
                  }
              });
          }
      };
      $('.layui-form .layui-form-item .layui-btn').on('click', function(){
          var type = $(this).data('type');
          active[type] ? active[type].call(this) : '';
      });


  });
</script>
</body>
</html>